package cn.benben.mybatis.handler;

import cn.benben.core.domain.BaseEntity;
import cn.benben.core.utils.SecurityUtils;
import cn.benben.core.utils.StringUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 自动填充时间字段
 *
 * @author xn
 */
@Component
public class CustomerMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {


//        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
//        this.strictInsertFill(metaObject, "createBy", String.class, SecurityUtils.getUsername());
//        this.strictInsertFill(metaObject, "createId", Long.class, SecurityUtils.getUserId());
//        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
//        this.strictInsertFill(metaObject, "updateBy", String.class, SecurityUtils.getUsername());
//        this.strictInsertFill(metaObject, "updateId", Long.class, SecurityUtils.getUserId());

        fillCreate(metaObject);
        fillUpdate(metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
//        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
//        this.strictInsertFill(metaObject, "updateBy", String.class, SecurityUtils.getUsername());
//        this.strictInsertFill(metaObject, "updateId", Long.class, SecurityUtils.getUserId());
        fillUpdate(metaObject);
    }


    private void fillUpdate(MetaObject metaObject) {
        // 修改人 修改时间
        if (metaObject.getOriginalObject() instanceof BaseEntity) {
            update(metaObject);
            return;
        }

        if (metaObject.hasGetter(BaseEntity.UPDATE_BY)) {
            Object oldVal = metaObject.getValue(BaseEntity.UPDATE_BY);
            if (oldVal == null) {
                this.setFieldValByName(BaseEntity.UPDATE_BY, SecurityUtils.getUsername(), metaObject);
            }
        }
        if (metaObject.hasGetter(BaseEntity.UPDATE_TIME)) {
            Object oldVal = metaObject.getValue(BaseEntity.UPDATE_TIME);
            if (oldVal == null) {
                this.setFieldValByName(BaseEntity.UPDATE_TIME, new Date(), metaObject);
            }
        }
        if (metaObject.hasGetter(BaseEntity.UPDATE_ID)) {
            Object oldVal = metaObject.getValue(BaseEntity.UPDATE_ID);
            if (oldVal == null) {
                this.setFieldValByName(BaseEntity.UPDATE_ID, SecurityUtils.getUserId(), metaObject);
            }
        }
    }

    private void fillCreate(MetaObject metaObject) {
        // 设置创建时间和创建人
        if (metaObject.getOriginalObject() instanceof BaseEntity) {
            create(metaObject);
            return;
        }
        if (metaObject.hasGetter(BaseEntity.CREATE_BY)) {
            Object oldVal = metaObject.getValue(BaseEntity.CREATE_BY);
            if (oldVal == null) {
                this.setFieldValByName(BaseEntity.CREATE_BY, SecurityUtils.getUsername(), metaObject);
            }
        }
        if (metaObject.hasGetter(BaseEntity.CREATE_TIME)) {
            Object oldVal = metaObject.getValue(BaseEntity.CREATE_TIME);
            if (oldVal == null) {
                this.setFieldValByName(BaseEntity.CREATE_TIME, new Date(), metaObject);
            }
        }
        if (metaObject.hasGetter(BaseEntity.CREATE_ID)) {
            Object oldVal = metaObject.getValue(BaseEntity.CREATE_ID);
            if (oldVal == null) {
                this.setFieldValByName(BaseEntity.CREATE_ID, SecurityUtils.getUserId(), metaObject);
            }
        }

    }

    private void create(MetaObject metaObject) {
        BaseEntity entity = (BaseEntity) metaObject.getOriginalObject();
        if (entity.getCreateTime() == null) {
            this.setFieldValByName(BaseEntity.CREATE_TIME, new Date(), metaObject);
        }
        if (entity.getCreateBy() == null) {
            Object userName = StringUtils.STRING_TYPE_NAME.equals(metaObject.getGetterType(BaseEntity.CREATE_BY).getName()) ? SecurityUtils.getUsername() : SecurityUtils.getUsername();
            this.setFieldValByName(BaseEntity.CREATE_BY, userName, metaObject);
        }
        if (entity.getCreateId() == null) {
            Object userIdVal = StringUtils.LONG_TYPE_NAME.equals(metaObject.getGetterType(BaseEntity.CREATE_ID).getName()) ? SecurityUtils.getUserId() : SecurityUtils.getUserId();
            this.setFieldValByName(BaseEntity.CREATE_ID, userIdVal, metaObject);
        }
    }

    private void update(MetaObject metaObject) {
        BaseEntity entity = (BaseEntity) metaObject.getOriginalObject();
        if (entity.getUpdateBy() == null) {
            Object userName = StringUtils.STRING_TYPE_NAME.equals(metaObject.getGetterType(BaseEntity.UPDATE_BY).getName()) ? SecurityUtils.getUsername() : SecurityUtils.getUsername();
            if (userName != null) {
                this.setFieldValByName(BaseEntity.UPDATE_BY, userName, metaObject);
            }
        }
        if (entity.getUpdateTime() == null) {
            this.setFieldValByName(BaseEntity.UPDATE_TIME, new Date(), metaObject);
        }
        if (entity.getUpdateId() == null) {
            Object userIdVal = StringUtils.LONG_TYPE_NAME.equals(metaObject.getGetterType(BaseEntity.UPDATE_ID).getName()) ? SecurityUtils.getUserId() : SecurityUtils.getUserId();
            if (userIdVal != null) {
                this.setFieldValByName(BaseEntity.UPDATE_ID, userIdVal, metaObject);
            }
        }
    }

}
